package com.blb.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.community.entity.SysDept;
import com.blb.community.entity.SysDictData;
import com.blb.community.entity.SysUser;
import com.blb.community.mapper.SysDeptMapper;
import com.blb.community.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Autowired
    private SysDeptMapper sysDeptMapper;


    @Override
    public List<SysDept> getDeptList(SysDept sysDept) {
        if (sysDept.getDeptName().equals("") && sysDept.getStatus().equals("")) {
            List<SysDept> deptList = sysDeptMapper.getDeptList(sysDept);

            return deptList;
        } else {
            /**
             * 模糊查询
             */
            List<SysDept> oneList = sysDeptMapper.getOneList(sysDept);

            return oneList;
        }

    }

    /**
     * 查询状态
     *
     * @param dictId 字典主键
     * @return
     */
    @Override
    public List<SysDictData> getDict(Long dictId) {
        return sysDeptMapper.getDict(dictId);
    }

    @Override
    public String addSysDept(SysDept sysDept) {
        SysDept sysDept1 = sysDeptMapper.getByNameAndId(sysDept);
        if (sysDept1 == null) {
            String ancestors = ancestors(sysDept);
            System.out.println(ancestors);
            sysDept.setAncestors(ancestors);
            sysDeptMapper.addSysdept(sysDept);
        } else {
            return "添加失败";
        }
        return "添加成功";
    }

    /**
     * 修改部门
     *
     * @param sysDept
     * @return
     */
    @Override
    public String updateDept(SysDept sysDept) {

        /**
         * 查询是否部门下是否有人员
         */
        List<SysUser> userByDeptId = sysDeptMapper.getUserByDeptId(sysDept.getDeptId());

        /**
         * 查询部门名是否已存在
         */
        SysDept byNameAndId = sysDeptMapper.getByNameAndId(sysDept);
        /**
         * 查询部门下是否有子部门
         */
        List<SysDept> sonDept = sysDeptMapper.getSonDept(sysDept.getDeptId());

        if (!sysDept.getStatus().equals("0")) {
            if (sonDept.size() != 0) {
                return "有子部门";
            }
        }

        if (!sysDept.getStatus().equals("0")) {
            if (userByDeptId.size() != 0) {
                return "部门有人";
            } else if (sysDept.getStatus().equals("0")) {
                sysDeptMapper.updateStatus(sysDept.getDeptId());
                return "状态设置成功";
            }
        }

        if (sysDept.getStatus().equals("1")) {
            sysDeptMapper.updateStatus1(sysDept.getDeptId());
            return "状态设置成功";
        }

        if (!sysDept.getStatus().equals("0")) {
            if (byNameAndId != null) {
                return "部门名已存在，不可重复";
            }
        }
        sysDeptMapper.updateDept(sysDept);
        return "修改成功";
    }

    /**
     * 删除
     *
     * @param deptId
     */
    @Override
    public String deleteDept(long deptId) {
        /**
         * 查询是否部门下是否有人员
         */
        List<SysUser> userByDeptId = sysDeptMapper.getUserByDeptId(deptId);

        /**
         * 查询部门下是否有子部门
         */
        List<SysDept> sonDept = sysDeptMapper.getSonDept(deptId);
        if (sonDept.size() != 0) {
            return "有子部门";
        }
        if (userByDeptId.size() != 0) {
            return "部门有人";
        }
        sysDeptMapper.deleteDept(deptId);
        return null;
    }

    /**
     * 处理ancestors
     */
    private String ancestors(SysDept sysDept) {
        String ancestors = "";
        if (sysDept.getParentId() == 0) {
            ancestors += 0;
        } else {
            ancestors += 0;
            SysDept deptById = sysDeptMapper.getById(sysDept.getParentId());
            if (deptById.getParentId() == 0) {
                ancestors = ancestors + "," + deptById.getDeptId();
            } else {
                SysDept deptById1 = sysDeptMapper.getById(deptById.getParentId());
                if (deptById1.getParentId() == 0) {
                    ancestors = ancestors + "," + deptById1.getDeptId() + "," + deptById.getDeptId();
                }
            }
        }
        return ancestors;
    }


}
